var users = new Array(); //存放所有用户的数组

// 创建用户的构造方法
function user(code, userName, sex, birthday, email, remark) {
    this.code = code;
    this.userName = userName;
    this.sex = sex;
    this.birthday = birthday;
    this.email = email;
    this.remark = remark;
}

// 刷新表格
function refresh(users) {
    var tbody = document.getElementById("tbody");
    var html = "";
    var color = "warning";
    var flag = true;
    for (var i in users) {
        if (flag) {
            color = "info";
        } else {
            color = "warning";
        }
        html = html + "<tr class='" + color + "'><td style='width:30px;'><input type='checkbox'></td><td id='code'>"
            + users[i].code + "</td><td id='userName'>"
            + users[i].userName + "</td><td id='sex'>"
            + users[i].sex + "</td><td id='birthday'>"
            + users[i].birthday + "</td><td id='email'>"
            + users[i].email + "</td><td id='remark'>"
            + users[i].remark + "</td>"
            + "</tr>";
        flag = !flag;//颜色转换
    }
    tbody.innerHTML = html; // 在表格中创建所有tr元素
}

// 要删除的id
let delids = "";
//删除用户
function del() {
    var checkboxs = $("input[type='checkbox']");  // 获取所有复选框
    let f = false;  // 是否选中用户的标记
    for (var i = 0; i < checkboxs.length; i++) {
        var box = checkboxs[i];  
        if (box.checked) {  // 某个复选框被选中
            f = true;  // 标记 有 复选框被选中
            box = $(box); // jsdom对象转换成jquery对象
            let code = box.parent().siblings("#code").text(); // 复选框父级的兄弟#code元素
            delids = delids + ";" + code;
        }
    }
    location.href = "deleteStudentServlet?ids=" + delids;
    alert("删除成功！");
    // button.innerHTML = html;
    if(!f){
        alert("请选择一个用户！");
    }
}

var code; //被修改的用户编号
//编辑用户
function edit() {
    var checkboxs = $("input[type='checkbox']");
    let f = false;
    for (var i = 0; i < checkboxs.length; i++) {
        var box = checkboxs[i];
        if (box.checked) {
            f = true;
            $(this).attr("data-toggle","modal");  // 启用模态框
            $(this).attr("data-target","#myModal"); // 启用模态框
            box = $(box);
            code = box.parent().siblings("#code").text();
            // 获取表格中选中的用户的各项数据
            var userName = box.parent().siblings("#userName").text();
            var sex = box.parent().siblings("#sex").text();
            var birthday = box.parent().siblings("#birthday").text();
            var email = box.parent().siblings("#email").text();
            var remark = box.parent().siblings("#remark").text();
            // 选中用户的各项数据，放在模态框的相应input框中
            $("#m_code").val(code);
            $("#m_userName").val(userName);
            $("#m_sex").val(sex);
            $("#m_birthday").val(birthday);
            $("#m_email").val(email);
            $("#m_remark").val(remark);
        }
    }
    if(!f){
        alert("请选择一个用户！");
        $(this).attr("data-toggle",null); // 禁用模态框
        $(this).attr("data-target",null); // 禁用模态框
    }
}


